package com.windowes.weichang.service;

import com.google.gson.Gson;
import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dto.EBPayRespDTO;
import com.windowes.weichang.dto.EBRequestParam;
import com.windowes.weichang.utils.pay.ZGTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/8/1 下午10:41
 * @description:
 *
 **/
@Slf4j
@Service
public class PayService {


    public ServerResponse pay(EBRequestParam ebRequestParam) {
        //获取各个请求参数
        Map<String, String> params = new HashMap<String, String>();
        params.put("requestid", "eb_requestid_" + ebRequestParam.getRequestid());
        params.put("amount", ebRequestParam.getAmount());
        params.put("assure", ebRequestParam.getAssure());
        params.put("productname", ebRequestParam.getProductname());
        params.put("productcat", ebRequestParam.getProductcat());
        params.put("productdesc", ebRequestParam.getProductdesc());
        params.put("divideinfo", ebRequestParam.getDivideinfo());
        params.put("callbackurl", ebRequestParam.getCallbackurl());
        params.put("webcallbackurl", ebRequestParam.getWebcallbackurl());
        params.put("bankid", ebRequestParam.getBankid());
        params.put("period", ebRequestParam.getPeriod());
        params.put("memo", ebRequestParam.getMemo());
        params.put("orderexpdate", ebRequestParam.getOrderexpdate());
        params.put("payeename", ebRequestParam.getPayeename());
        params.put("payproducttype", ebRequestParam.getPayproducttype());
        params.put("userno", ebRequestParam.getUserno());
        params.put("ip", "");
        params.put("cardname", ebRequestParam.getCardname());
        params.put("idcard", ebRequestParam.getIdcard());
        params.put("bankcardnum", ebRequestParam.getBankcardnum());
        params.put("mobilephone", ebRequestParam.getMobilephone());
        params.put("appid", ebRequestParam.getAppid());
        params.put("openid", ebRequestParam.getOpenid());
        params.put("directcode", ebRequestParam.getDirectcode());
        params.put("platform", ebRequestParam.getPlatform());
        params.put("appname", ebRequestParam.getAppid());
        params.put("appstatement", ebRequestParam.getAppstatement());

        log.info("请求参数:{}", new Gson().toJson(params));
        //第一步 生成密文data
        String data = ZGTUtils.buildData(params, ZGTUtils.PAYAPI_REQUEST_HMAC_ORDER);

        //第二步 发起请求
        String requestUrl = ZGTUtils.getRequestUrl(ZGTUtils.PAYAPI_NAME);
        Map<String, String> responseMap = ZGTUtils.httpPost(requestUrl, data);

        //第三步 判断请求是否成功，
        if (responseMap.containsKey("code")) {
            return ServerResponse.createByErrorMessage("支付异常:code:" + responseMap.get("code") + " msg:" + responseMap.get("msg"));
        }

        //第四步 解密同步响应密文data，获取明文参数
        String responseData = responseMap.get("data");
        Map<String, String> responseDataMap = ZGTUtils.decryptData(responseData);
        log.info("data解密后明文：" + responseDataMap);

        //第五步 code=1时，方表示接口处理成功
        String toJson = new Gson().toJson(responseDataMap);
        if (!"1".equals(responseDataMap.get("code"))) {
            log.error(toJson);
            return ServerResponse.createByErrorMessage(responseDataMap.get("msg"));
        }

        //第六步 hmac签名验证
        if (!ZGTUtils.checkHmac(responseDataMap, ZGTUtils.PAYAPI_RESPONSE_HMAC_ORDER)) {
            return ServerResponse.createByErrorMessage("验签失败");
        }

        //第七步 进行业务处理
        //当支付方式为微信扫码，展示二维码
        EBPayRespDTO payRespDTO = new Gson().fromJson(toJson, EBPayRespDTO.class);
        return ServerResponse.createBySuccess(payRespDTO);
    }
    
}
